package com.zmc.admin.controller.system;

import com.zmc.admin.application.RoleApplicationService;
import com.zmc.admin.infrastructure.bean.vo.user.SysRoleVo;
import com.zmc.admin.infrastructure.entity.SysRoleEntity;
import com.zmc.admin.infrastructure.entity.SysUserEntity;
import com.zmc.admin.infrastructure.entity.SysUserRoleEntity;
import com.zmc.admin.infrastructure.bean.query.SysRoleQuery;
import com.zmc.admin.infrastructure.bean.query.SysUserQuery;
import com.zmc.admin.infrastructure.service.SysDeptService;
import com.zmc.admin.infrastructure.service.SysRoleService;
import com.zmc.admin.infrastructure.service.SysUserService;
import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 角色信息
 * @author zmc
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private SysRoleService roleService;

    @Autowired
    private AuthTokenService authTokenService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysDeptService deptService;

    @Autowired
    private RoleApplicationService roleApplicationService;

    @PostMapping("/list")
    public R list(@RequestBody SysRoleQuery query) {
        PageUtils page = roleService.pageRoleList(query);
        return R.page(page);
    }

    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, SysRoleQuery query) {
        List<SysRoleEntity> list = roleService.selectRoleList(query);
        ExcelUtil<SysRoleEntity> util = new ExcelUtil<>(SysRoleEntity.class);
        util.exportExcel(response, list, "角色数据");
    }

    /**
     * 根据角色编号获取详细信息
     */
    @PostMapping("/info")
    public R getInfo(@RequestBody SysRoleVo vo) {
        return R.ok(roleService.getById(vo.getRoleId()));
    }

    /**
     * 新增角色
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R add(@RequestBody SysRoleEntity role) {
        roleApplicationService.addRole(role);
        return R.ok();

    }

    /**
     * 修改保存角色
     */
    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R edit(@RequestBody SysRoleEntity role) {
        roleApplicationService.updateRole(role);
        return R.ok();
    }

    /**
     * 修改保存数据权限
     */
    @PostMapping("/dataScope")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R dataScope(@RequestBody SysRoleEntity role) {
        roleService.authDataScope(role);
        return R.ok();
    }

    /**
     * 状态修改
     */
    @PostMapping("/changeStatus")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R changeStatus(@RequestBody SysRoleEntity role) {
        role.setUpdateBy(authTokenService.getUserInfo().getUsername());
        roleService.updateById(role);
        return R.ok();
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete")
    public R remove(@RequestBody SysRoleVo vo) {
        roleApplicationService.removeRole(vo.getRoleId());
        return R.ok();
    }

    /**
     * 获取角色选择框列表
     */
    @GetMapping("/optionselect")
    public R optionselect() {
        return R.ok(roleService.list());
    }

    /**
     * 查询已分配用户角色列表
     */
    @PostMapping("/authUser/allocatedList")
    public R allocatedList(@RequestBody SysUserQuery user) {
        List<SysUserEntity> list = userService.selectAllocatedList(user);
        return R.page(list);
    }

    /**
     * 查询未分配用户角色列表
     */
    @PostMapping("/authUser/unallocatedList")
    public R unallocatedList(@RequestBody SysUserQuery user) {
        List<SysUserEntity> list = userService.selectUnallocatedList(user);
        return R.page(list);
    }

    /**
     * 取消授权用户
     */
    @PostMapping("/authUser/cancel")
    public R cancelAuthUser(@RequestBody SysUserRoleEntity userRole) {
        roleService.deleteAuthUser(userRole);
        return R.ok();
    }

    /**
     * 批量取消授权用户
     */
    @PostMapping("/authUser/cancelAll")
    public R cancelAuthUserAll(@RequestBody SysRoleVo vo) {
        roleService.deleteAuthUsers(vo.getRoleId(), vo.getUserIds());
        return R.ok();
    }

    /**
     * 批量选择用户授权
     */
    @PostMapping("/authUser/selectAll")
    public R selectAuthUserAll(@RequestBody SysRoleVo vo) {
        roleService.insertAuthUsers(vo.getRoleId(), vo.getUserIds());
        return R.ok();
    }

    /**
     * 获取对应角色部门树列表
     */
    @PostMapping(value = "/deptTree")
    public R deptTree(@RequestBody SysRoleVo vo) {
        return Objects.requireNonNull(R.ok().put("checkedKeys", deptService.selectDeptListByRoleId(vo.getRoleId())))
                .put("depts", deptService.list());
    }
}
